package com.km.team.phsystem.service.impl;

import com.km.team.phsystem.beans.pojo.SystemCommonConf;
import com.km.team.phsystem.beans.pojo.SystemCommonConfExample;
import com.km.team.phsystem.beans.vo.SystemCommonConfVo;
import com.km.team.phsystem.dao.mybatis.SystemCommonConfMapper;
import com.km.team.phsystem.service.IBusiTypeService;
import com.km.team.phsystem.service.impl.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: rochzhu
 * Date: 2018/10/18
 * Time: 19:00pm
 * Description:
 */
@Service("busiTypeService")
public class BusiTypeServiceImpl extends BaseService<BusiTypeServiceImpl, SystemCommonConf> implements IBusiTypeService {

    @Autowired
    private SystemCommonConfMapper systemCommonConfMapper;

    @Transactional
    @Override
    public int add(SystemCommonConfVo systemCommonConfVo) throws Exception {
        SystemCommonConf conf = new SystemCommonConf();
        conf.setId(0);
        conf.setSysType("business_type");
        conf.setSysValue(systemCommonConfVo.getSys_value());
        conf.setNote(systemCommonConfVo.getNote());
        return systemCommonConfMapper.insert(conf);
    }

    @Transactional
    @Override
    public int update(SystemCommonConf systemCommonConf) throws Exception {
        if(systemCommonConf==null){
            return 0;
        }
//        return systemCommonConfMapper.updateByPrimaryKey(systemCommonConf);
        return systemCommonConfMapper.updateByPrimaryKeySelective(systemCommonConf);
    }

    @Transactional
    @Override
    public int delete(SystemCommonConf systemCommonConf) throws Exception {
        if(systemCommonConf==null){
            return 0;
        }
        return systemCommonConfMapper.deleteByPrimaryKey(systemCommonConf.getId());
    }

    @Override
    public List<SystemCommonConf> getBusiTypeData() throws Exception {
        SystemCommonConfExample example = new SystemCommonConfExample();
        SystemCommonConfExample.Criteria criteria = example.createCriteria();
        criteria.andSysTypeEqualTo("business_type");
        return systemCommonConfMapper.selectByExample(example);
    }

    @Override
    public Map<String, String> getBusiTypeMap() throws Exception {
        Map<String, String> bizTypeMap = new HashMap<String, String>();
        SystemCommonConfExample example = new SystemCommonConfExample();
        SystemCommonConfExample.Criteria criteria = example.createCriteria();
        criteria.andSysTypeEqualTo("business_type");

        List<SystemCommonConf> confs =  systemCommonConfMapper.selectByExample(example);
        for(SystemCommonConf conf : confs){
            bizTypeMap.put(conf.getId() + "" , conf.getSysValue());
        }
        return bizTypeMap;
    }

    @Override
    public List<SystemCommonConf> getBusiType(SystemCommonConfVo systemCommonConfVo) throws Exception {
        SystemCommonConfExample example = new SystemCommonConfExample();
        SystemCommonConfExample.Criteria criteria = example.createCriteria();
        criteria.andSysValueEqualTo(systemCommonConfVo.getSys_value());
        return systemCommonConfMapper.selectByExample(example);
    }


    @Override
    public List<SystemCommonConf> getSmsCapacity() throws Exception {
        SystemCommonConfExample example = new SystemCommonConfExample();
        SystemCommonConfExample.Criteria criteria = example.createCriteria();
        criteria.andSysTypeEqualTo("sms_capacity");
        return systemCommonConfMapper.selectByExample(example);
    }

    @Override
    public List<SystemCommonConf> getSmsSendTime() throws Exception {
        SystemCommonConfExample example = new SystemCommonConfExample();
        SystemCommonConfExample.Criteria criteria = example.createCriteria();
        criteria.andSysTypeEqualTo("sms_sendtime");
        return systemCommonConfMapper.selectByExample(example);
    }

    @Override
    public  List<SystemCommonConf> getSmsArriveTime() throws Exception {
        SystemCommonConfExample example = new SystemCommonConfExample();
        SystemCommonConfExample.Criteria criteria = example.createCriteria();
        criteria.andSysTypeEqualTo("sms_arrivetime");
        return systemCommonConfMapper.selectByExample(example);
    }
}
